﻿using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System;

namespace GeekShop
{
    public class FileTypeManager
    {
        #region Fields
        private OpenFileDialog openFileDialog = new OpenFileDialog();
        private SaveFileDialog saveFileDialog = new SaveFileDialog();
        private ImageCodecInfo[] encoders;
        private ImageCodecInfo[] decoders;
        #endregion

        #region Properties
        public OpenFileDialog OpenFileDialog
        {
            get { return openFileDialog; }
            set { openFileDialog = value; }
        }
        public SaveFileDialog SaveFileDialog
        {
            get { return saveFileDialog; }
            set { saveFileDialog = value; }
        }
        public ImageCodecInfo[] Encoders
        {
            get { return encoders; }
        }
        public ImageCodecInfo[] Decoders
        {
            get { return decoders; }
        }
        #endregion

        #region Constructors
        public FileTypeManager() {
            this.encoders = ImageCodecInfo.GetImageEncoders();
            this.decoders = ImageCodecInfo.GetImageDecoders();

            // Create SaveFileDialog Filter
            string encoderFilter = "";
            foreach (ImageCodecInfo codecInfo in this.encoders)
            {
                encoderFilter += codecInfo.FormatDescription + "|" + codecInfo.FilenameExtension + "|";
            }
            this.saveFileDialog.Filter = encoderFilter.Substring(0, encoderFilter.Length - 1);

            // Create OpenFileDialog Filter
            string decoderFilter = "";
            string decoderExtensions = "";
            int decoderCount = 0;
            foreach (ImageCodecInfo codecInfo in this.decoders)
            {
                decoderFilter += codecInfo.FormatDescription + "|" + codecInfo.FilenameExtension + "|";
                decoderExtensions += codecInfo.FilenameExtension + ";";
                decoderCount++;
            }
            decoderFilter += "All Image Files|" + decoderExtensions.Substring(0, decoderExtensions.Length - 1) + "|All Files|*.*";
            this.openFileDialog.Filter = decoderFilter;
            this.openFileDialog.FilterIndex = decoderCount + 1;
        }
        #endregion

        #region Public Methods
        public ImageCodecInfo GetSelectedImageEncoderInfo()
        {
            return this.encoders[this.saveFileDialog.FilterIndex - 1];
        }
        public ImageCodecInfo GetSelectedImageDecoderInfo()
        {
            if (this.saveFileDialog.FilterIndex - 1 < this.encoders.Length)
            {
                return this.encoders[this.saveFileDialog.FilterIndex];
            }
            return null;
        }
        private ImageCodecInfo GetImageEncoderInfo(ImageFormat format)
        {
            foreach (ImageCodecInfo codecInfo in this.encoders)
            {
                if (codecInfo.FormatID == format.Guid)
                {
                    return codecInfo;
                }
            }
            return null;
        }
        private ImageCodecInfo GetImageDecoderInfo(ImageFormat format)
        {
            foreach (ImageCodecInfo codecInfo in this.decoders)
            {
                if (codecInfo.FormatID == format.Guid)
                {
                    return codecInfo;
                }
            }
            return null;
        }
        #endregion
    }
}
